package com.zsk.swing;

import java.awt.BorderLayout;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.List;

import javax.swing.JButton;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;

public class PswUI extends JFrame {
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private JTextField FileTxt;
	private JTextArea msgView;
	private JButton openBtn;
	private JButton decodeBtn;

	public void init() {
		setSize(800, 600);
		setTitle("����");
		setLocationRelativeTo(null);
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		// setLayout(new FlowLayout(FlowLayout.LEFT));
		JPanel northPanel = new JPanel();
		northPanel.setLayout(new FlowLayout(FlowLayout.LEFT));
		// setResizable(false);
		JLabel note = new JLabel("��������:");
		FileTxt = new JTextField(50);
		openBtn = new JButton("ѡ��");
		decodeBtn = new JButton("����");
		msgView = new JTextArea(25, 95);
		msgView.setEditable(false);
		JScrollPane scroll = new JScrollPane(msgView);

		addEvent();
		northPanel.add(note);
		northPanel.add(FileTxt);
		northPanel.add(openBtn);
		northPanel.add(decodeBtn);
		add(northPanel, BorderLayout.NORTH);
		add(scroll, BorderLayout.CENTER);
		setVisible(true);
	}

	private void addEvent() {
		addOpenFileEvent();
		addDecodeEvent();
	}

	private void addOpenFileEvent() {
		openBtn.addActionListener(new ActionListener() {
			private String lastPath="";
			public void actionPerformed(ActionEvent e) {
				JFileChooser jfc = null;
				if (lastPath.length()>0) {
					jfc=new JFileChooser(lastPath);
				}
				else {
					jfc=new JFileChooser();
				}
				jfc.setFileSelectionMode(JFileChooser.FILES_AND_DIRECTORIES);
				jfc.showDialog(new JLabel(), "ѡ��");
				File file = jfc.getSelectedFile();
				if (file==null) {
					return;
				}
				if (file.isDirectory()) {
					addMsg("ѡ���ļ��У�" + file.getAbsolutePath());
				} else if (file.isFile()) {
					addMsg("ѡ���ļ���" + file.getAbsolutePath());
				}
				lastPath=jfc.getSelectedFile().getAbsolutePath();
				FileTxt.setText(lastPath);
			}

		});
	}

	private void addDecodeEvent() {
		decodeBtn.addActionListener(new ActionListener() {

			public void actionPerformed(ActionEvent e) {

				try {
					File file = new File(FileTxt.getText());
					if (!file.exists()) {
						showWarningDialog("��ѡ���ļ�");
						return;
					}

					if (file.isFile()) {
						File newFile=new File(new File("").getCanonicalPath()+"/"+file.getName()+"_");
						copyFile(file,newFile);
						showInfoDialog("���ܳɹ�");
						addMsg("�����ļ�·��:"+newFile.getCanonicalPath());
					} else if (file.isDirectory()) {

					}

				} catch (Exception e1) {
					showWarningDialog("��ѡ���ļ�");
				}

			}
		});
	}

	private void showWarningDialog(String msg) {
		JOptionPane.showMessageDialog(PswUI.this, msg, "����", JOptionPane.WARNING_MESSAGE);
	}
	private void showInfoDialog(String msg) {
		JOptionPane.showMessageDialog(PswUI.this, msg, "��Ϣ", JOptionPane.INFORMATION_MESSAGE);
	}

	private void addMsg(String msg) {
		msgView.append(msg + "\n");
	}

	private static void listFiles(String dir, List<String> fileList) {
		// ���ָ���ļ�����
		File file = new File(dir);
		// ��ø��ļ����ڵ������ļ�
		File[] array = file.listFiles();

		for (int i = 0; i < array.length; i++) {
			if (array[i].isFile())// ������ļ�
			{
				try {
					fileList.add(array[i].getCanonicalPath());
				} catch (IOException e) {
					e.printStackTrace();
				}
			} else if (array[i].isDirectory())// ������ļ���
			{
				listFiles(array[i].getPath(), fileList);
			}
		}
	}

	private static void copyFile(File fromFile, File toFile) throws IOException {
		FileInputStream ins = new FileInputStream(fromFile);
		FileOutputStream out = new FileOutputStream(toFile);
		byte[] b = new byte[1024];
		int n = 0;
		while ((n = ins.read(b)) != -1) {
			out.write(b, 0, n);
		}
		ins.close();
		out.close();
	}

	public static void main(String[] args) {
		new PswUI().init();
	}
}
